<?php
defined('IN_PHPCMS') or exit('No permission resources.');
class order_detail {
	function __construct() {
		pc_base::load_app_func('global');
		$this->order_detail = pc_base::load_model('order_detail_model');//投票标题
		$this->username = param::get_cookie('_username');
		$this->userid = param::get_cookie('_userid');
		$this->ip = ip();

		$siteid = isset($_GET['siteid']) ? intval($_GET['siteid']) : get_siteid();
  		define("SITEID",$siteid);
  		
  		if (!$this->userid) {
  			$this->output(false, '您没有权限进行此操作，请您先登录!', APP_PATH.'index.php?m=member&c=index&a=login');
  		}
 	}
 	
 	public function add()
 	{
 		$in = array_merge($_GET, $_POST);
 		$in = new_addslashes($in);
 		
 		//查询首选清单
 		$order_model= pc_base::load_model('order_model');
 		$order = $order_model->get_one('`userid`='.$this->userid.' AND selected=1');
 		if (!$order) {
 			$this->output(false, '请您先创建清单号！');
 		}
 		
 		$content_model = pc_base::load_model('content_model');
 		$content_model->set_model(13);
 		$product = $content_model->get_one('id='.$in['productid']);
 		if (!$product) {
 			$this->output(false, '抱歉，不存在此产品！');
 		}
 		
 		//栏目
 		$CATEGORYS = getcache('category_content_'.SITEID,'commons');
 		$cat = $CATEGORYS[$product['catid']];
 		$arrparentid = explode(',', $cat['arrparentid']);
 		$topcat = $CATEGORYS[$arrparentid[2]];
 		
 		//品牌
 		$top_bid = (empty($product['bids']) ? '' : array_shift(explode(',', $product['bids'])));
		if ($top_bid) {
			$brand_db = pc_base::load_model('brand_model');
			$brand = $brand_db->get_one(array('id'=>$top_bid));
		}
 		
 		if ($brand['userid']) {
 			$member_model = pc_base::load_model('member_model');
	 		$member = $member_model->get_one('userid='.$brand['userid']);
	 		$member_model->set_model($member['modelid']);
	 		$member_ext = $member_model->get_one(array('userid' => $brand['userid']));
	 		$member = array_merge($member, $member_ext);
 			if ($member['vip'] == 1 && $member['overduedate'] > SYS_TIME) {
	 			$member_vip = true;
	 		}
 		}
 		
 		//查询此清单已有数据数量
 		$has_num = $this->order_detail->count(array('oid' => $order['oid'], 'userid' => $this->userid));
 		if ($has_num > 200) {
 			$this->output(false, '添加失败，每个清单最多200个产品！');
 		}
 		
 		//检测是否已存在于此清单中
 		if ($this->order_detail->count(array('oid' => $order['oid'], 'userid' => $this->userid, 'productid' => intval($in['productid'])))) {
 			$this->output(true, '加入清单成功！');
 		}
 		
 		//插入数据
 		$data = array(
 			'oid'	=> $order['oid'],
 			'ordertitle'	=> $order['title'],
 			'topcatid'	=>	$topcat['catid'],
 			'catid'	=> $product['catid'],
 			'bid'	=> ($top_bid ? $top_bid : ''),
 			'userid'	=> $this->userid,
 			'username'	=> $this->username,
 			'supplierid'	=> isset($brand['userid']) ? $brand['userid'] : '',
 			'nickname'	=> (isset($brand['prealname']) && $member_vip) ? $brand['prealname'] : '',
 			'suppliername' => (isset($member['username']) && $member_vip) ? $member['username'] : '',
 			'companyname' => (isset($brand['pcompanyname']) && $member_vip) ? $brand['pcompanyname'] : '',
 			'qq' => (isset($brand['pqq']) && $member_vip) ? $brand['pqq'] : '',
 			'tel' => (isset($brand['ptel']) && $member_vip) ? $brand['ptel'] : '',
 			'productid'	=>	intval($in['productid']),
 			'producttitle'	=>	$product['title'],
 			'thumb'		=> substr($product['thumb'], strpos($product['thumb'], 'uploadfile/')),
 			'note'		=> '',
 			'inputtime'	=>	time()
 		);
 		
 		//查找上一次添加的产品备注信息
 		$old_order_detail = $this->order_detail->get_one(array('userid'=>$this->userid, 'productid'=>intval($in['productid'])), 'note');
 		if ($old_order_detail && $old_order_detail['note']!='') {
 			$data['note'] = $old_order_detail['note'];
 		}
 		
 		if ($insertid = $this->order_detail->insert($data, true)) {
 			//更新加入清单次数
 			$ordernum = $product['ordernum'] + 1;
 			$monthordernum = (date('Ym', $product['updatetime']) == date('Ym', SYS_TIME)) ? ($product['monthordernum'] + 1) : 1;
 			$sql = array('ordernum'=>$ordernum,'monthordernum'=>$monthordernum,'updatetime'=>SYS_TIME);
			$content_model->update($sql, array('id'=>$product['id']));

 			$this->output(true, '加入清单成功！');
 		} else {
 			$this->output(true, '加入清单失败！');
 		}
 	}
 	
/**
	 * 我的清单  详情
	 * eddy
	 */
	public function lists() {
		$member_model = pc_base::load_model('member_model');
		$memberinfo = $member_model->get_one(array('userid' => $this->userid));

		//关联栏目  eddy
		//$category_model = pc_base::load_model('category_model');
		//$memberinfo['category'] = $category_model->get_one(array('userid' => $this->userid));

		if (isset($_GET['id']) && intval($_GET['id'])) {
			$id = intval($_GET['id']);
			$order_model = pc_base::load_model('order_model');
			$order_model->update(array('look'=>1), 'id='.$id);  //更新为已读状态
			$order = $order_model->get_one('id='.$id);
			
			if (!$order) {
				showmessage("没有此清单~", 'index.php?m=order&c=index&a=lists');
			}
			if ($order['userid'] != $this->userid) {
				showmessage("抱歉，此清单不是您所创建的~", 'index.php?m=member&c=index&a=order');
			}
			
			$order_detail_model = pc_base::load_model('order_detail_model');
			$where = array('oid' => $order['oid']);
			/*if(!isset($_GET['topcatid'])) {
				$cat_db = pc_base::load_model('category_model');
				$topcat = $cat_db->get_one(array('parentid'=>TOP_PRODUCT_CATID), 'catid');
				$_GET['topcatid'] = $topcat['catid'];
			}*/
			
			if (isset($_GET['topcatid']) && $_GET['topcatid'] > 0) {
				$_GET['topcatid'] = intval($_GET['topcatid']);
				$where['topcatid'] = intval($_GET['topcatid']);
			} 
			$content_total = $order_detail_model->count($where);
			$page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
			$pagesize = 10;
			$offset = ($page - 1) * $pagesize;
			
			$order_detail_pages = pages($content_total, $page, $pagesize);
			
			$order_detail_data = $order_detail_model->select($where, '*', $offset.",".$pagesize, 'updatetime DESC');

			//获取顶部分类
			$categorys = getcache('category_content_'.SITEID,'commons');
			$topcategorys = array();
			foreach ($categorys AS $catid=>$cat) {
				if ($cat['parentid'] == 7) {
					if($siteid && $cat['siteid']!=$siteid) continue;
					if (strpos($cat['url'], '://') === false) {
						$cat['url'] = substr($site['domain'],0,-1).$cat['url'];
					}
					$cat['productnum'] = 0;
					$topcategorys[$catid] = $cat;
				}
			}
			
			//计算每个分类下的产品数量
			$order_detail_data2 = $order_detail_model->select(array('oid' => $order['oid']), '*', "200", 'updatetime DESC');
			foreach ($order_detail_data2 AS &$order_detail) {
				if (isset($topcategorys[$order_detail['topcatid']])) {
					$topcategorys[$order_detail['topcatid']]['productnum']++;
				}
			}
			
 		
			$SEO['title'] = '我的清单 - 会员中心';
			include template('order', 'order_detail');
		} else {
			showmessage("参数错误~", 'index.php?m=member&c=index&a=order');
		}
	}
	
 	
	/**
	 * 更新备注
	 * Enter description here ...
	 * @param unknown_type $code
	 * @param unknown_type $msg
	 * @param unknown_type $url
	 */
	public function update_note()
	{
		$in = new_addslashes(array_merge($_GET, $_POST));
		$this->order_detail->update(array('note'=> trim($in['note'])), array('id' => intval($in['id'])));
		$this->output(true, "备注更新成功！");
	}
	
	/**
	 * 删除
	 * Enter description here ...
	 */
	public function delete() 
	{
		$in = new_addslashes(array_merge($_GET, $_POST));
		if (isset($in['id']) && intval($in['id'])) {
			$id = intval($in['id']);
			$this->order_detail->delete(array('id'=>$id));
			$this->output(true, '删除成功！');
		}
	}
	
	/**
	 * 询价
	 * Enter description here ...
	 * @param unknown_type $code
	 * @param unknown_type $msg
	 * @param unknown_type $url
	 */
	public function update_price()
	{
		$in = new_addslashes(array_merge($_GET, $_POST));
		if (!isset($in['id'])) {
			$this->output(false, '参数错误！');
		}
		
		$id = intval($in['id']);
		$order_detail = $this->order_detail->get_one(array('id'=>$id));
		switch ($order_detail['enquiry']) {
			case 0:  //开始询价
				$this->order_detail->update(array('enquiry'=>1, 'enquirytime' => SYS_TIME), array('id'=>$id));
				$this->output(true, '询价成功，等待供应商提供价格！');
				break;
			case 1:  //回复询价
			case 2:
				if (!isset($in['price'])) {
					$this->output(false, '您提供的价格数据不规范！');
				}
				
				$price = floatval($in['price']);
				$this->order_detail->update(array('enquiry'=>2, 'price'=>$price), array('id'=>$id));
				$this->output(true, '价格发送成功！');
				break;
		}
	}
	
	
	protected function output($code, $msg, $url='') {
		$in = array_merge($_GET, $_POST);
		if($in['ajax']) {
			die(json_encode(array('code' => $code, 'msg' => $msg)));
		} else {
			$forward = !empty($url) ? $url : HTTP_REFERER;
			showmessage($msg, $forward);
		}
	}
}
?>